<template>
  <div v-loading="loading">
    <a-card>
      <title-name title="合同评审汇总（公司）" />
      <a-form-model
        ref="ruleForm"
        :model="form3"
        :rules="form3Rules"
      >
        <a-row>
          <a-col span="24">
            <a-form-model-item label="实施单位">
              <BaseInput
                disabled
                value="中国建筑第八工程局有限公司西南分公司"
              />
            </a-form-model-item>
          </a-col>
          <a-col span="8">
            <a-form-model-item label="项目名称">
              <BaseInput
                disabled
                :value="baseForm.projectName"
              />
            </a-form-model-item>
          </a-col>
          <a-col span="8">
            <a-form-model-item label="建设单位">
              <BaseInput
                disabled
                :value="baseForm.otherParty"
              />
            </a-form-model-item>
          </a-col>
          <a-col span="8">
            <a-form-model-item label="合同额（万元）">
              <BaseInput
                disabled
                :value="baseForm.amount"
              />
            </a-form-model-item>
          </a-col>
          <a-col span="8" v-if="!baseForm.linearIs">
            <a-form-model-item
              label="总建筑面积（㎡）"
              prop="totalArea"
            >
              <BaseInputNumber
                v-model="form3.totalArea"
                placeholder="请输入"
                :min="0"
                :precision="2"
              />
            </a-form-model-item>
          </a-col>
          <a-col span="8" v-if="!baseForm.linearIs">
            <a-form-model-item label="其中地下建筑面积（㎡）">
              <BaseInputNumber
                v-model="form3.undergroundArea"
                placeholder="请输入"
                :min="0"
                :precision="2"
              />
            </a-form-model-item>
          </a-col>
          <a-col span="8" v-if="!baseForm.linearIs">
            <a-form-model-item label="结构形式" prop="structureStyleTypeId">
              <BSelect v-model="form3.structureStyleTypeId" :request="()=>getTypeLabel(30)"/>
            </a-form-model-item>
          </a-col>
          <a-col span="8" v-if="!baseForm.linearIs">
            <a-form-model-item
              label="层数（地下+地上）"
              prop="floor"
            >
              <BaseInputNumber
                v-model="form3.floor"
                is-int
                placeholder="请输入"
                :min="0"
              />
            </a-form-model-item>
          </a-col>
          <a-col span="8" v-if="baseForm.linearIs">
            <a-form-model-item label="合同工程里程" prop="totalKm">
              <BaseInputNumber v-model="form3.totalKm" :precision="2" placeholder="请输入" :min="0" />
            </a-form-model-item>
          </a-col>
          <a-col span="8">
            <a-form-model-item
              label="质量等级"
              prop="qualityLevel"
            >
              <BaseInput
                v-model="form3.qualityLevel"
                placeholder="请输入"
              />
            </a-form-model-item>
          </a-col>
          <a-col span="8">
            <a-form-model-item
              label="工期（月）"
              prop="constructionPeriod"
            >
              <BaseInputNumber
                v-model="form3.constructionPeriod"
                placeholder="请输入"
                is-int
                :min="0"
              />
            </a-form-model-item>
          </a-col>
          <a-col span="8">
            <a-form-model-item label="预期利润率(%)（不含税）">
              <BaseInputNumber
                v-model="form3.predictProfitRate"
                placeholder="请输入"
                :min="0"
                :precision="2"
              />
            </a-form-model-item>
          </a-col>
          <a-col span="8">
            <a-form-model-item label="承包方式" prop="contractModeTypeId">
              <BSelect v-model="form3.contractModeTypeId" :request="()=>getTypeLabel(32)"/>
            </a-form-model-item>
          </a-col>
          <a-col span="8">
            <a-form-model-item label="承包范围">
              <BaseInput
                v-model="form3.scope"
                placeholder="请输入"
              />
            </a-form-model-item>
          </a-col>
          <a-col span="8">
            <a-form-model-item
              label="合同类型"
              prop="comReviewTypeId"
            >
              <a-select
                v-model="form3.comReviewTypeId"
                placeholder="请选择"
              >
                <a-select-option
                  v-for="item of selectOptionList.companyContractType"
                  :key="item.id"
                  :value="item.id"
                >
                  {{ item.name }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col span="8">
            <a-form-model-item
              label="合同等级"
              prop="comReviewLevelId"
            >
              <a-select
                v-model="form3.comReviewLevelId"
                placeholder="请选择"
              >
                <a-select-option
                  v-for="item of selectOptionList.companyContractLevel"
                  :key="item.id"
                  :value="item.id"
                >
                  {{ item.name }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col span="24">
            <a-form-model-item
              label="工程地点"
              prop="address"
            >
              <BaseTextarea
                v-model="form3.address"
                placeholder="请输入"
              />
            </a-form-model-item>
          </a-col>
          <a-col
            v-for="item of form3.options"
            :key="item.id"
            span="24"
          >
            <a-form-model-item :label="item.name">
              <a-row>
                <a-col
                  v-for="child of item.children"
                  :key="child.id"
                  span="24"
                >
                  <a-checkbox
                    v-model="child.hasSelected"
                    :value="child.id"
                  >
                    {{ child.name }}
                  </a-checkbox>
                </a-col>
              </a-row>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-card>
    <attachment
      :attachment="form3.fileInfos"
      @change="(e) => (form3.fileInfos = e)"
    />
    <div class="mr20p page-btn-right-top">
    <!-- <div class="mr20p mb20p tar"> -->
      <template v-if="form3.comReview !== 'SUBMITTED'">
        <a-button
          v-if="form3.comReview == 'REJECTED'"
          type="primary"
          class="mr10p"
          @click="save(form3.comReview)"
        >
          保存
        </a-button>
        <a-button
          v-else
          type="primary"
          class="mr10p"
          @click="save('DRAFT')"
        >
          保存草稿
        </a-button>
        <a-button
          type="primary"
          class="mr10p"
          @click="submit"
        >
          提交
        </a-button>
      </template>
      <a-button @click="$close()">
        关闭
      </a-button>
    </div>
    <selectApprover :businessId="businessId"  :processConfig="setProcessConfig"></selectApprover>
  </div>
</template>
<script>
//api接口
import totalPackageContractApi from '@/api/legal/branchContract/totalPackageContract'
import attachment from './attachment'
export default {
    name: 'ContractReview',
    components: {
        attachment
    },
    props: {
        baseForm: {
            type: Object
        },
        selectOptionList: {
            type: Object,
            default() {
                return {}
            }
        }
    },
    data() {
        return {
          getTypeLabel: type => totalPackageContractApi.typeslabel({ pageNo: 0, pageSize: 100000, query: { type } }),
          loading: false,
            form3: { fileInfos: [] },
            form3Rules: {
              totalKm: [{ required: true, message: '请输入合同工程里程', trigger: 'blur' }],
                totalArea: [{ required: true, message: '请输入总建筑面积（㎡）', trigger: 'blur' }],
                undergroundArea: [{ required: true, message: '请输入其中地下建筑面积（㎡）', trigger: 'blur' }],
              structureStyleTypeId: [{ required: true, message: '请选择结构形式', trigger: 'change' }],
                floor: [{ required: true, message: '请输入层数（地下+地上）', trigger: 'blur' }],
                qualityLevel: [{ required: true, message: '请输入质量等级', trigger: 'blur' }],
                constructionPeriod: [{ required: true, message: '请输入工期（月）', trigger: 'blur' }],
                predictProfitRate: [{ required: true, message: '请输入预期利润率(%)（不含税）', trigger: 'blur' }],
              contractModeTypeId: [{ required: true, message: '请输入承包方式', trigger: 'blur' }],
                scope: [{ required: true, message: '请输入承包范围', trigger: 'blur' }],
                address: [{ required: true, message: '请输入工程地点', trigger: 'blur' }],
                comReviewTypeId: [{ required: true, message: '请选择合同类型', trigger: 'change' }],
                comReviewLevelId: [{ required: true, message: '请选择合同等级', trigger: 'change' }]
            },
            //获取流程配置值
            processConfig: null,
            //点击提交发送流程配置值
            setProcessConfig: null,
            businessId: null
        }
    },
   async created() {
        this.getData()
        this.processConfig = await this.$getConfigFn('legal_general_company_new')
    },
    methods: {
        async getData() {
            this.loading = true
            if (this.baseForm.baseId) {
                let res = await totalPackageContractApi.findComReviewByBaseId(
                    {},
                    {
                        baseId: this.baseForm.baseId,
                        edit: true
                    }
                )
                if (res.code === 200) {
                    res.data.comReviewTypeId = res.data.comReviewTypeId || undefined
                    res.data.comReviewLevelId = res.data.comReviewLevelId || undefined
                    this.form3 = res.data
                    if (!this.form3.options) {
                        let res = await totalPackageContractApi.findCompanyTreeLabel()
                        if (res.code === 200) {
                            this.form3.options = res.data
                        }
                    }

                    console.log('this.from3',this.form3)
                    console.log('baseform',this.baseForm)
                }
            }
            this.loading = false
        },
        async submit() {
            this.submitApprovalConfirm(this.processConfig, (flag)=>{
              if(!flag) {
                return
              }
              this.save('SUBMITTED')
            })

            // if (!await this.save('DRAFT')) {
            //     return
            // }
            // this.$bus.$emit('submit', '1103', '0')
        },
        isValidate() {
            let flag = true

            this.$refs.ruleForm.validate((valid, object) => {
                flag = valid
            })
            return flag
        },
        async save(status = 'DRAFT') {
            this.$console(this.form3)
            return new Promise(async (resolve) => {
                if (status == 'SUBMITTED') {
                    if (!this.isValidate()) return
                }
                this.loading = true
                this.form3.baseId = this.baseForm.baseId
                this.form3.comReview = status
                let res = await totalPackageContractApi.updateComReview(this.form3)
                if (res.code === 200) {
                    this.getData()
                    resolve(true)
                    if (status === 'SUBMITTED') {
                        this.$noticeSuccess('提交成功')
                        this.businessId = res.data?.businessId;
                        this.setProcessConfig = this.processConfig;
                        //this.$multiTab.close(this.$route.fullpath)
                    } else {
                        this.$noticeSuccess('保存成功')
                    }
                }
                this.loading = false
                resolve(false)
            })
        }
    }
}
</script>

<style scoped>
</style>
